import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { STColumn, STComponent, STData } from '@delon/abc/st';
import { SFSchemaEnum, SFSelectWidgetSchema } from '@delon/form';
import { SearchDrawerService } from '@shared';
import { of } from 'rxjs';
import { map } from 'rxjs/operators';
import { BasicTableComponent } from 'src/app/routes/commom';
import { VehicleService } from '../../../vehicle/services/vehicle.service';
@Component({
  selector: 'app-Vehicle-components-list',
  templateUrl: './list.component.html',
  styleUrls: ['../../../commom/less/commom-table.less']
})
export class VehicleComponentsListComponent extends BasicTableComponent implements OnInit {
  columns!: STColumn[];

  constructor(
    public service: VehicleService,
    private router: Router,
    private ar: ActivatedRoute,
    public searchDrawerService: SearchDrawerService
  ) {
    super(searchDrawerService);
    this.sfValue = { enterpriseName: '', contactName: '' };
  }

  /**
   * 查询参数
   */
  get reqParams() {
    const params: any = {
      ...this.sfValue
    };
    if (this.sfValue?.effectiveDate) {
      params.effectiveDateStart = this.sfValue?.effectiveDate[0];
      params.effectiveDateEnd = this.sfValue?.effectiveDate[1];
    }
    delete params.effectiveDate;
    delete params.expand;
    return params;
  }

  search() {
    this.st?.load(1);
  }

  ngOnInit() {
    this.initSF();
    this.initST();
  }
  dataProcess(data: STData[]): STData[] {
    return data.map((i, index) => {
      i.showSortFlag = false;
      return i;
    });
  }
  initSF() {
    this.schema = {
      properties: {
        expand: {
          type: 'boolean',
          ui: {
            hidden: true
          }
        },
        carNo: { title: '车牌号', maxLength: 9, type: 'string', ui: { showRequired: false, placeholder: '请输入车牌号' } },
        carNoColor: {
          type: 'string',
          title: '车牌颜色',
          ui: {
            widget: 'dict-select',
            containsAllLabel: true,
            params: { dictKey: 'car:color' }
          }
        },
        carStatus: {
          title: '运营状态',
          type: 'string',
          enum: [
            { label: '空闲', value: 0 },
            { label: '运输中', value: 1 }
          ],
          ui: {
            allowClear: true,
            widget: 'select'
          }
        },
        carModel: {
          title: '车型',
          type: 'string',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'car:model' },
            containsAllLabel: true
          }
        },
        carLength: {
          title: '车长',
          type: 'string',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'car:length' },
            containsAllLabel: true
          }
        },
        carLoad: {
          title: '载重',
          type: 'string'
        },
        isSelf: {
          type: 'string',
          title: '是否挂靠',
          enum: [
            { label: '是', value: 1 },
            { label: '否', value: 0 }
          ],
          ui: {
            widget: 'select',
            allowClear: true
          }
        },
        driverLicenseStatus: {
          type: 'string',
          title: '行驶证到期状态',
          enum: [
            { label: '正常', value: 1 },
            { label: '即将到期', value: 2 },
            { label: '已到期', value: 3 }
          ],
          default: '',
          ui: {
            widget: 'select',
            allowClear: true
          }
        },
        roadTransportStatus: {
          type: 'string',
          title: '道运证到期状态',
          enum: [
            { label: '正常', value: 1 },
            { label: '即将到期', value: 2 },
            { label: '已到期', value: 3 }
          ],
          ui: {
            widget: 'select',
            allowClear: true
          }
        }
        // isSelfs: {
        //   type: 'string',
        //   title: '是否入网',
        //   enum: [
        //     { label: '全部', value: '' },
        //     { label: '是', value: 1 },
        //     { label: '否', value: 0 }
        //   ],
        //   ui: {
        //     widget: 'select',
        //     allowClear: true,
        //     visibleIf: {
        //       expand: (value: boolean) => value
        //     }
        //   }
        // }
        // putOnRecord: {
        //   type: 'string',
        //   title: '是否已备案',
        //   enum: [
        //     { label: '是', value: 1 },
        //     { label: '否', value: 0 },
        //   ],
        //   ui: {
        //     widget: 'select',
        //     visibleIf: {
        //       expand: (value: boolean) => value,
        //     },
        //   },
        // },
      }
    };
  }

  initST() {
    this.columns = [
      // { title: '', type: 'checkbox', className: 'text-center' },
      {
        title: '序号',
        className: 'text-center',
        width: '60px',
        format: (item: STData, col: STColumn, idx: number) => {
          return ((this.st.pi -1) * this.st.ps) + idx + 1 + ''
        } 
      },
      { title: '车牌号', width: '150px', className: 'text-center', index: 'carNo' },
      { title: '车牌颜色', width: '140px', className: 'text-center', index: 'carNoColorLabel' },
      { title: '车辆品牌', width: '140px', className: 'text-center', index: 'carBrand' },
      { title: '车型-车长-载重', width: '180px', className: 'text-center', render: 'carLength' },
      {
        title: '运营状态',
        width: '150px',
        className: 'text-center',
        index: 'carStatus',
        type: 'badge',
        badge: {
          true: { text: '运输中', color: 'success' },
          false: { text: '空闲', color: 'default' }
        }
      },
      // { title: '道运证', width: '150px', className: 'text-center', index: 'roadTransportNo' },
      // { title: '审核人', width: '150px', className: 'text-center', index: 'approvalUserName' },

      {
        title: '行驶证到期状态',
        width: '180px',
        className: 'text-center',
        index: 'driverLicenseStatus',
        type: 'badge',
        badge: {
          1: { text: '正常', color: 'success' },
          2: { text: '即将到期', color: 'warning' },
          3: { text: '已到期', color: 'error' }
        }
      },
      {
        title: '道运证到期状态',
        width: '180px',
        className: 'text-center',
        index: 'roadTransportStatus',
        type: 'badge',
        badge: {
          1: { text: '正常', color: 'success' },
          2: { text: '即将到期', color: 'warning' },
          3: { text: '已到期', color: 'error' }
        }
      },
      // { title: '是否入网', width: '200px', className: 'text-center', index: 'carOwner' },
      { title: '所有人', width: '200px', className: 'text-center', index: 'carOwner' },
      { title: '审核人', width: '200px', className: 'text-center', index: 'approvalUserName' },
      { title: '审核通过时间', width: '200px', className: 'text-center', index: 'createTime' },
      { title: '资料更新时间', width: '200px', className: 'text-center', index: 'modifyTime' },
      { title: '申请时间', width: '200px', className: 'text-center', index: 'applyTime' },
      { title: '录入人员', width: '200px', className: 'text-center', index: 'saveUser' },
      { title: '是否挂靠', width: '150px', className: 'text-center', render: 'isSelf' },
      { title: '挂靠协议', width: '150px', className: 'text-center', render: 'approvalAuditStatus' },
      // { title: '是否已备案', className: 'text-center', render: 'putOnRecord', },
      {
        title: '操作',
        fixed: 'right',
        width: '100px',
        className: 'text-center',
        buttons: [
          {
            text: '查看',
            acl: { ability: ['VEHICLE-LIST-view'] },
            click: item => {
              this.router.navigate(['/vehicle/list/detail/' + item.id]);
              // this.router.navigate(['./view', item.id], { relativeTo: this.ar, queryParams: { tenantId: item.tenantId } });
            }
          }
          // {
          //   text: '申请备案',
          //   click: (item) => {

          //   },
          // },
        ]
      }
    ];
  }
  creat() {
    this.router.navigate(['./new'], { relativeTo: this.ar });
  }
  // 导出
  exportFire() {
    let params = Object.assign({}, this.reqParams || {});
    params.pageSize = -1;
    this.service.exportStart(params, this.service.$api_carLicense_export);
  }
}
